import { generateRandomId, timeoutTask, isEndWithX } from '@/utils/index';
import { routes } from '@/router/index';
import app from './app';

const state = {
  liveId: '',
  emceeId: '',
  sceneType: '0',
  title: '',
  background: '',
  qrcodeUrl: '',
  giftTitleList: [], // 互动2.0礼物标题列表
  giftNavIndex: 0,
  giftList_format01: [], // 互动2.0格式化礼物列表01
  giftList_format02: [], // 互动2.0格式化礼物列表02
  giftList_format03: [], // 互动2.0格式化礼物列表03
  giftList01: [], // 互动2.0礼物列表01
  giftList02: [], // 互动2.0礼物列表01
  giftList03: [], // 互动2.0礼物列表01
  giftList_format: [], // 经过格式化后的礼物列表
  giftList: [], // 礼物列表
  rcGiftList: [], // 推荐礼物列表
  chatList: [], // 聊天记录(包括发送礼物记录、聊天记录)
  giftQueue: [], // 礼物队列
  currentGiftEffect: { // 当前展示的礼物效果
    id: '',
    path: '',
    imgPath: '',
    giftName: '',
    userName: '',
    headImg: '',
  },
  photoTypeList: [], // 照片类型列表
  danmuTypeList: [], // 霸气弹幕类型列表
  bapinTypeList: [], // 霸屏类型列表
  superDanmuTypeList: [], // 超级弹幕类型列表
  rechargeList: [], // 充值列表
  hbkdRechargeList: [], // 红包口袋充值列表
  remainMoney: 0, // 红包口袋余额
  zyzList: '',
  isFreeSendPhoto: false, // 是否是免费发照片礼物场次
  wifi: '', // 无线网密码
  broadContent: '', // 广播通知
  logoPath: '', // 酒吧logo
  onlineNum: 0, // 在线人数
  beerJson: '', // 啤酒大作战游戏配置
};

const mutations = {
  setLiveId: (state, data) => {
    state.liveId = data;
  },
  setEmceeId: (state, data) => {
    state.emceeId = data;
  },
  setSceneType: (state, data) => {
    console.log('***setSceneType***');
    console.log(data);
    state.sceneType = data;
  },
  setZyzList: (state, data) => {
    state.zyzList = data;
  },
  setTitle: (state, data) => {
    state.title = data;
    routes[0].meta.title = state.title;
    routes[1].meta.title = state.title;
    if (!(isEndWithX(window.location.href, 'photoWall') || isEndWithX(window.location.href, 'photographerWall'))) {
      document.title = state.title;
    }
  },
  setBackground: (state, data) => {
    state.background = data || state.background;
  },
  setGiftList: (state, data) => {
    state.giftList_format = data.giftList_format;
    state.giftList = data.giftList;
    state.rcGiftList = data.giftList.filter((item) => {
      return item.is_pop === '1';
    }).map((item) => {
      return {
        giftImg: item.imglink,
        giftName: item.giftname,
        price: item.giftprice,
        giftId: item.giftconst,
      };
    });
    console.log('****推荐礼物*****');
    console.log(state.rcGiftList);
  },
  setGiftList2: (state, data) => {
    state.giftTitleList = data.giftTitleList;
    state.giftList_format01 = data.giftList_format01;
    state.giftList_format02 = data.giftList_format02;
    state.giftList_format03 = data.giftList_format03;
    state.giftList01 = data.giftList01;
    state.giftList02 = data.giftList02;
    state.giftList03 = data.giftList03;
    state.rcGiftList = data.giftList01.filter((item) => {
      return item.is_pop === '1';
    }).map((item) => {
      return {
        giftImg: item.imglink,
        giftName: item.giftname,
        price: item.giftprice,
        giftId: item.giftconst,
      };
    });
    console.log('****推荐礼物*****');
    console.log(state.rcGiftList);
  },
  setGiftNavIndex: (state, data) => {
    state.giftNavIndex = data;
  },
  initChatList: (state, data) => {
    state.chatList = data;
  },
  addChat: (state, data) => {
    if (state.chatList.length > 30) {
      state.chatList.shift();
    }
    state.chatList.push(data);
    timeoutTask(() => {
      app.state.chatAreaScrollId = data.id;
    }, 200);
  },
  setPhotoTypeList: (state, data) => {
    state.photoTypeList = data;
  },
  setDanmuTypeList: (state, data) => {
    state.danmuTypeList = data;
  },
  setSuperDanmuTypeList: (state, data) => {
    state.superDanmuTypeList = data;
  },
  setBapinTypeList: (state, data) => {
    state.bapinTypeList = data;
  },
  setRechargeList: (state, data) => {
    state.rechargeList = data.split(',');
  },
  setHbkdRechargeList: (state, data) => {
    state.hbkdRechargeList = data;
  },
  setHbkdInfo: (state, data) => {
    state.remainMoney = data.remainMoney;
  },
  addToGiftQueue: (state, data) => {
    if (state.currentGiftEffect.id === '') {
      state.currentGiftEffect = {
        id: generateRandomId(),
        path: data.coupontype,
        imgPath: data.imglink,
        giftName: data.giftname,
        userName: data.userName,
        headImg: data.headImg,
        shijian: data.shijian,
      };
    } else {
      const list = state.giftQueue;
      list.push(data);
      state.giftQueue = list;
    }
  },
  removeFromGiftQueue: (state) => {
    const list = state.giftQueue;
    if (list.length === 0) {
      state.currentGiftEffect = {
        id: '',
        path: '',
        imgPath: '',
        giftName: '',
        userName: '',
        headImg: '',
        shijian: '',
      };
    } else {
      const g = list.shift();
      state.giftQueue = list;
      state.currentGiftEffect = {
        id: generateRandomId(),
        path: g.coupontype,
        imgPath: g.imglink,
        giftName: g.giftname,
        userName: g.userName,
        headImg: g.headImg,
        shijian: g.shijian,
      };
    }
  },
  setQrcodeUrl: (state, data) => {
    state.qrcodeUrl = data;
  },
  setIsFreeSendPhoto: (state, data) => {
    if (data === true || data === false) {
      state.isFreeSendPhoto = data;
    }
  },
  setWifi: (state, data) => {
    state.wifi = data;
  },
  setBroadContent: (state, data) => {
    state.broadContent = data;
  },
  setLogoPath: (state, data) => {
    state.logoPath = data;
  },
  setOnlineNum: (state, data) => {
    state.onlineNum = data;
  },
  setBeerJson: (state, data) => {
    if (!data) {
      return;
    }
    state.beerJson = data;
  },
};

const actions = {
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
